package com.example.medicalequipment.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.medicalequipment.R;
import com.example.medicalequipment.entity.Device;
import com.example.medicalequipment.ui.activity.DeviceDetailActivity;
import com.example.medicalequipment.ui.adapter.DeviceCategoryAdapter;
import com.example.medicalequipment.ui.adapter.DeviceListAdapter;

import java.util.ArrayList;
import java.util.List;

public class DeviceFragment extends Fragment {

    private ListView lvCategories, lvDevices;
    private TextView tvTitle;
    private List<String> categoryList;
    private List<Device> deviceList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_device, container, false);

        lvCategories = view.findViewById(R.id.lvCategories);
        lvDevices = view.findViewById(R.id.lvDevices);
        tvTitle = view.findViewById(R.id.tvTitle);

        initCategories();
        initDeviceList();

        DeviceCategoryAdapter categoryAdapter = new DeviceCategoryAdapter(requireContext(), categoryList);
        lvCategories.setAdapter(categoryAdapter);

        // 默认显示全部设备
        updateDeviceList("全部");

        lvCategories.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedCategory = categoryList.get(position);
                updateDeviceList(selectedCategory);
                tvTitle.setText(selectedCategory + "设备");
            }
        });

        lvDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Device selectedDevice = ((DeviceListAdapter) lvDevices.getAdapter()).getItem(position);
                navigateToDeviceDetail(selectedDevice);
            }
        });

        return view;
    }

    private void initCategories() {
        categoryList = new ArrayList<>();
        categoryList.add("全部");
        categoryList.add("肝胆外科");
        categoryList.add("影像科");
        categoryList.add("病理科");
        categoryList.add("放射科");
        categoryList.add("心内科");
        categoryList.add("检验科");
    }

    private void initDeviceList() {
        deviceList = new ArrayList<>();
        String[] departments = {"影像科", "放射科", "肝胆外科", "病理科", "检验科", "心内科"};

        // 生成20个设备（部分科室重复）
        deviceList.add(new Device("X光拍片机", departments[1], R.drawable.devices_1, "RAD-001",
                "基础X光拍摄", "用于骨骼、胸部等基础影像检查"));
        deviceList.add(new Device("数字化X线机(DR)", departments[1], R.drawable.devices_2, "RAD-002",
                "数字化X光拍摄", "相比传统X光机，成像更清晰、辐射更低"));
        deviceList.add(new Device("CT扫描仪", departments[1], R.drawable.devices_3, "RAD-003",
                "断层扫描成像", "提供人体横断面的详细影像"));

        deviceList.add(new Device("B超机", departments[0], R.drawable.devices_4, "IM-001",
                "黑白超声检查", "用于腹部、妇科等常规超声检查"));
        deviceList.add(new Device("彩超机", departments[0], R.drawable.devices_5, "IM-002",
                "彩色多普勒超声", "支持血流成像的高级超声检查"));
        deviceList.add(new Device("三维超声仪", departments[0], R.drawable.devices_6, "IM-003",
                "三维立体成像", "提供胎儿产前筛查等三维影像"));

        deviceList.add(new Device("电子腹腔镜", departments[2], R.drawable.devices_7, "SUR-001",
                "微创手术设备", "肝胆外科常用的腹腔内诊疗工具"));
        deviceList.add(new Device("超声刀", departments[2], R.drawable.devices_8, "SUR-002",
                "手术切割止血", "配合腹腔镜使用的精准切割设备"));
        deviceList.add(new Device("胆道镜", departments[2], R.drawable.devices_9, "SUR-003",
                "胆道疾病诊疗", "用于胆管结石、肿瘤等胆道系统检查"));

        deviceList.add(new Device("全自动切片机", departments[3], R.drawable.devices_10, "PATH-001",
                "病理切片制备", "自动切取组织样本，提高病理检测效率"));
        deviceList.add(new Device("冰冻切片机", departments[3], R.drawable.devices_11, "PATH-002",
                "快速病理切片", "用于术中快速冰冻病理诊断"));
        deviceList.add(new Device("组织脱水机", departments[3], R.drawable.devices_12, "PATH-003",
                "组织样本处理", "病理样本脱水、透明、浸蜡等预处理"));

        deviceList.add(new Device("全自动生化分析仪", departments[4], R.drawable.devices_13, "LAB-001",
                "血液生化检测", "检测肝功能、肾功能、电解质等指标"));
        deviceList.add(new Device("血细胞分析仪", departments[4], R.drawable.devices_14, "LAB-002",
                "血常规检测", "自动分析血液中各类细胞数量及形态"));
        deviceList.add(new Device("尿液分析仪", departments[4], R.drawable.devices_15, "LAB-003",
                "尿液常规检测", "快速分析尿液中的化学成分和有形成分"));

        deviceList.add(new Device("心电监护仪", departments[5], R.drawable.devices_16, "CAR-001",
                "生命体征监测", "实时监测心电图、心率、血压、血氧等"));
        deviceList.add(new Device("除颤仪", departments[5], R.drawable.devices_9, "CAR-002",
                "心脏急救设备", "用于心室颤动、心搏骤停等紧急情况"));
        deviceList.add(new Device("动态心电图仪", departments[5], R.drawable.devices_13, "CAR-003",
                "24小时心电监测", "连续记录患者24小时的心电图变化"));
    }

    private void updateDeviceList(String category) {
        List<Device> filteredDevices = new ArrayList<>();
        if ("全部".equals(category)) {
            filteredDevices.addAll(deviceList);
        } else {
            for (Device device : deviceList) {
                if (category.equals(device.getDepartment())) {
                    filteredDevices.add(device);
                }
            }
        }
        DeviceListAdapter deviceAdapter = new DeviceListAdapter(requireContext(), filteredDevices);
        lvDevices.setAdapter(deviceAdapter);
    }

    private void navigateToDeviceDetail(Device device) {
        Intent intent = new Intent(requireContext(), DeviceDetailActivity.class);
        intent.putExtra("device", device);
        startActivity(intent);
    }
}